package com.exam.controller;

import com.exam.entity.EClass;
import com.exam.entity.EClassUser;
import com.exam.entity.common.ResponseResult;
import com.exam.service.EClassUserService;
import com.exam.util.JsonUtils;
import com.exam.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
public class EClassUserController {

    @Autowired
    @Qualifier("eClassUserServiceImpl")
    private EClassUserService eClassUserService;
    ResponseResult responseResult=new ResponseResult();


    @RequestMapping("/get_class_user_list_by_c_id")
    @ResponseBody
    public String get_class_user_list_by_c_id(int c_id){
        List<EClassUser> classUserListByCId = eClassUserService.get_class_user_list_by_c_id(c_id);
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(classUserListByCId);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/del_class_user_list_by_cu_id")
    @ResponseBody
    public String del_class_user_list_by_cu_id(int cu_id){
        int i = eClassUserService.del_class_user_list_by_cu_id(cu_id);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/get_not_in_class_user_by_c_id")
    @ResponseBody
    public String get_not_in_class_user_by_c_id(int c_id){
        List<Map<String, Object>> notInClassUserByCId = eClassUserService.get_not_in_class_user_by_c_id(c_id);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(notInClassUserByCId);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/add_class_user")
    @ResponseBody
    public String add_class_user(int c_id,String u_ids){
        String[] u_id_data = u_ids.split(",");
        List<EClassUser> eClassUsers=new ArrayList<>();
        for (String u_id : u_id_data) {
            EClassUser eClassUser = new EClassUser();
            eClassUser.setC_id(c_id);
            eClassUser.setU_id(Integer.parseInt(u_id));
            eClassUsers.add(eClassUser);
        }
        int i = eClassUserService.add_class_user(eClassUsers);
        if (i>0){
            responseResult.setCode(200);
            responseResult.setMessage("添加成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);

    }


}

